// 全局组件魔法注释
const IMPORT_COMMON_COMPONENT = '/** plop import common compontent **/'
const IMPORT_COMMON_COMPONENT_NAME =
  '/** plop import common compontent name **/'

// 全局页面路由魔法注释
const VIEW_LAYOUT_ROUTER = '/** plop view layout router **/'
const VIEW_PAGE_ROUTER = '/** plop view page router **/'
module.exports = plop => {
  plop.setHelper('upperCase', function(text) {
    return text.toUpperCase()
  })
  // 创建组件
  plop.setGenerator('component', {
    // 描述文件
    description: 'create a component',
    prompts: [{
      type: 'input',
      name: 'name',
      message: 'component name',
      default: 'myComponent'
    },
    // 选择 是项目组件 or 通用组件 😺
    {
      type: 'list',
      name: 'type',
      message: 'common or project?',
      choices: [{
        name: 'common',
        checked: true
      }, {
        name: 'project',
        checked: false
      }],
      default: 'project'
    }
    ],
    actions(option) {
      let actions = []
      if (option.type === 'project') {
        actions = actions.concat([{
          type: 'add', // 类型添加
          path: 'src/components/project/{{camelCase name}}.vue',
          templateFile: 'plop-templates/component/index.hbs'
        }])
      } else {
        // 全局注册 公共组件
        actions = actions.concat([{
          type: 'add', // 类型添加
          path: 'src/components/common/{{properCase name}}/index.vue',
          templateFile: 'plop-templates/component/index.hbs'
        },
        {
          type: 'append',
          pattern: IMPORT_COMMON_COMPONENT,
          path: 'src/globalEl/index.js',
          template: 'import {{ properCase name}} from "@/components/common/{{properCase name}}/index";'
        },
        {
          type: 'append',
          pattern: IMPORT_COMMON_COMPONENT_NAME,
          path: 'src/globalEl/index.js',
          template: '{{ properCase name }},'
        }
        ])
      }
      return actions
    }
  })
  // 创建page
  plop.setGenerator('page', {
    description: 'create a page',
    prompts: [
      // 创建一个页面
      {
        type: 'input',
        name: 'name',
        message: 'page name',
        default: 'defaultPage'
      },
      {
        type: 'input',
        name: 'des',
        message: 'title',
        default: '一个普通的页面'
      },
      {
        type: 'checkbox',
        name: 'meta',
        message: 'set route meta 😺',
        choices: [{
          name: 'keepAlive',
          checked: true
        }, {
          name: 'customNav',
          checked: false
        }
        ]
      }
      // {
      //   type: 'list',
      //   name: 'type',
      //   message: 'page or layout?',
      //   choices: [{
      //     name: 'page',
      //     checked: true
      //   }, {
      //     name: 'loyout',
      //     checked: false
      //   }],
      //   default: 'page'
      // },
    ],
    actions(option) {
      option.customNav = option.meta.includes('customNav')
      option.keepAlive = option.meta.includes('keepAlive')
      let actions = []
      actions = actions.concat([{
        type: 'add', // 类型添加
        path: 'src/views/{{camelCase name}}/index.vue',
        templateFile: 'plop-templates/view/index.hbs'
      },
      {
        type: 'append',
        pattern: VIEW_PAGE_ROUTER,
        path: 'src/router/pageRoutes.js',
        templateFile: 'plop-templates/view/page-route-register.hbs'
      }
      ])
      return actions
    }
  })
  // 创建layout
  plop.setGenerator('layout', {
    description: 'create a layout',
    prompts: [
      // 创建一个页面
      {
        type: 'input',
        name: 'name',
        message: 'layout name',
        default: 'defaultLayout'
      },
      {
        type: 'input',
        name: 'des',
        message: 'title',
        default: '一个tabbar的页面'
      },
      {
        type: 'checkbox',
        name: 'meta',
        message: 'set route meta 😺',
        choices: [{
          name: 'keepAlive',
          checked: true
        }]
      }
    ],
    actions(option) {
      option.keepAlive = option.meta.includes('keepAlive')
      let actions = []
      actions = actions.concat([{
        type: 'add', // 类型添加
        path: 'src/views/{{camelCase name}}/index.vue',
        templateFile: 'plop-templates/view/index.hbs'
      },
      {
        type: 'append',
        pattern: VIEW_LAYOUT_ROUTER,
        path: 'src/router/tabberRoutes.js',
        templateFile: 'plop-templates/view/layout-route-register.hbs'
      }
      ])
      return actions
    }
  })
}
